package org.argetr.resim.proc.view;


import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;

import org.argetr.resim.proc.ConnectionRule;
import org.argetr.resim.proc.data.Data;
import org.argetr.resim.proc.data.HistogramDA;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYBarDataset;
import org.jfree.data.xy.XYDataset;

public class HistogramViewerPR extends ViewerPR {
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(HistogramDA.class, 0);
	}
	@Override
	public Data process() {
		HistogramDA data = getInputAsHistogramDA();
        int nBins = data.getNBins();
        int nBands = data.getNBands();
		for (int i = 0; i < nBands; i++) {
			DefaultXYDataset xySet = new DefaultXYDataset();
			double[][] points = new double[2][nBins];
			int[] histogram = data.getBins(i);
			for(int y = 0; y < nBins; y++){
					points[0][y] = y;
					points[1][y] = (double)histogram[y];
			}
			xySet.addSeries("HI", points);
			XYBarDataset series = new XYBarDataset(xySet, 2);
			JFreeChart chart = ChartFactory.createXYBarChart(
					"Histogram Chart", i + ". Band Bins", false, "Total Numbers", series,
					PlotOrientation.VERTICAL, true, false, false);
			XYPlot plot = (XYPlot) chart.getPlot();
			// set the range axis to display integers only...
			final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
			rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			ChartPanel panel = new ChartPanel(chart);
			JFrame frame = new JFrame();
			frame.setTitle("Histogram Viewer");
			Container contentPane = frame.getContentPane();
			contentPane.setLayout(new BorderLayout());
			frame.setContentPane(panel);
			frame.setBounds(0, 0, 500, 270);
			frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			frame.setVisible(true); // show the frame.
		}
		return null;
	}
	
}
